package com.peakproducts;

import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class ConnectionThread extends Thread {

    private boolean start = false;

    private boolean stop = false;

    private String url;

    private String data;

    public boolean sendResult = false;

    public boolean sending = false;
    
    private String requestMode = HttpConnection.POST;
    
    public String responseContent;

    public void run() {
        while (true) {
            if (start == false && stop == false) {
                try {
                    sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (stop) {
                return;
            } else if (start) {
                http();
            }
        }
    }
    
    private void getResponseContent( HttpConnection conn ) throws IOException {
        InputStream is = null;
        is = conn.openInputStream();

            // Get the length and process the data
            int len = (int) conn.getLength();
            if ( len > 0 ) {
                int actual = 0;
                int bytesread = 0;
                byte[] data = new byte[len];
                while ( ( bytesread != len ) && ( actual != -1 ) ) {
                    actual = is.read( data, bytesread, len - bytesread );
                    bytesread += actual;
                }
                responseContent = new String (data);
            } else {
                int ch;
                while ( ( ch = is.read() ) != -1 ) {

                }
            }
            
          
  
    }
    
    private void http() {
        System.out.println( url );
        HttpConnection conn = null;
        OutputStream out = null;
        int responseCode;

        try {
            conn = (HttpConnection) Connector.open(url);
            conn.setRequestMethod(requestMode);
            out = conn.openOutputStream();
            out.write(data.getBytes());
            out.flush();
            responseCode = conn.getResponseCode();

            if (responseCode != HttpConnection.HTTP_OK) {
                sendResult = false;
                responseContent = null;
            } else {
                sendResult = true;
                getResponseContent( conn );
            }
            start = false;
            sending = false;

        } catch (IOException e) {
            start = false;
            sendResult = false;
            sending = false;
        } 

    }

    public void get(String url) {
        this.url = url;
        this.data = "";
        requestMode = HttpConnection.GET;
        sendResult = false;
        sending = true;
        start = true;
    }
    
    public void post(String url, String data) {
        this.url = url;
        this.data = data;
        requestMode = HttpConnection.POST;
        sendResult = false;
        sending = true;
        start = true;
    }

    public void stop() {
        stop = true;
    }

}


